package com.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.entity.SysBlog;
import com.web.entity.SysPoster;
import com.web.entity.SysUser;
import com.web.service.SysBlogService;

@Controller
@RequestMapping("blog")
public class SysBlogController {

	@Autowired
	private SysBlogService sysBlogService;

	@RequestMapping("tap")
	public String Tappage() {
		return "manage/adminBlog";
	}

	@RequestMapping("addBlog")
	public String addBlog() {
		return "manage/AddBlog";
	}

	@RequestMapping("updateBlog")
	public String updateBlog(Model model, Integer id) {
		model.addAttribute("BlogId", sysBlogService.getBlogId(id));
		System.out.println(JSONObject.toJSON(sysBlogService.getBlogId(id)));
		return "manage/updateBlog";
	}

	@ResponseBody
	@RequestMapping("Bloglist")
	private PageInfo<SysUser> list(SysBlog sb, Integer pageNum, Integer pageSize) {

		return sysBlogService.selectShowBlogs(sb, pageNum, pageSize);
	}

	@ResponseBody
	@RequestMapping("selectBlog")
	public List<SysBlog> listBlog(Model model) {
		// model.addAttribute("blogs", sysBlogService.getBlogs());
		return sysBlogService.getBlogs();
	}

	// 前台的详情界面
	@RequestMapping("Details")
	public String blogs(@RequestParam("id") Integer id, Model model) {
		SysBlog blog = sysBlogService.getBlogId(id).get(0);
		model.addAttribute("blogs", blog);
		return "home/detBlogs";
	}

	// 改变状态
	@ResponseBody
	@RequestMapping("updateState")
	public Map<String, Object> updateState(@RequestParam("id") Integer id, @RequestParam("state") int state) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (state == 1) {
			state = 0;
		} else {
			state = 1;
		}
		// System.out.println(id+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+state);
		map = sysBlogService.changeBlogState(id, state);
		return map;
	}

	@ResponseBody
	@RequestMapping("delBlogRow")
	public Map<String, Object> delRow(@RequestParam("id") Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = sysBlogService.deleteBlog(id);
		return map;
	}

	@ResponseBody
	@RequestMapping("saveBlog")
	public Map<String, Object> saveBlog(SysBlog sysBlog) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = sysBlogService.addBlog(sysBlog);
		return map;
	}

	// 流式分页
	@ResponseBody
	@RequestMapping("getlist")
	public Map<String, Object> getlist(SysBlog sysBlog, Integer pageNum, Integer pageSize) {
		return sysBlogService.getBlogList(sysBlog, pageNum, pageSize);
	}

	// 普通分页
	@RequestMapping("pagingBlog")
	public String viewAll(Model model) {
		Map<String, Object> resuMap = sysBlogService.getBlogList(new SysBlog(), 1, 8);
		model.addAttribute("rows", resuMap.get("rows"));
		model.addAttribute("navigatepageNums", resuMap.get("navigatepageNums"));
		return "manage/pagingBlog";
	}

	// 修改博客内容
	@ResponseBody
	@RequestMapping("editorBlog")
	public Map<String, Object> editorBlog(SysBlog sysBlog) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = sysBlogService.editorBlog(sysBlog);
		return map;
	}

}
